﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ADA.Entity;
using ADA.Business;
using System.IO;

namespace ADA.Presentation.PRL.MarketNews
{
    public partial class EditMarketNewsForm : Form
    {
        bool add;
        int newsID;
        MainForm main;
        bool flag;
        public EditMarketNewsForm(MainForm Main)
        {
            main = Main;
            add = true;
            InitializeComponent();
        }

        public EditMarketNewsForm(int NewsID, MainForm Main)
        {
            main = Main;
            add = false;
            newsID = NewsID;
            InitializeComponent();
        }

        private void EditMarketNewsForm_Load(object sender, EventArgs e)
        {
            lblError.Text = "";
            if (add == true)
            {
                btnUpload.Visible = false;
                return;
            }
            string message;
            News news;
            BOLNews bolNews = new BOLNews();
            if (bolNews.getNewsInfo(newsID, out news, out message) == true)
            {
                tbxTitle.Text = news.Title;
                tbxTitleJP.Text = news.TitleJP;
                img.ImageLocation = Common.URL_NEWS + Path.GetFileName(news.Image);
                tbxContent.Text = news.Content;
                tbxContentJP.Text = news.ContentJP;
            }
            else
            {
                MessageBox.Show(message);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            lblError.Text = "";
            validate();
            if (flag == false) return;
            if (add == true)
            {
                addNews();
            }
            else
            {
                editNews();
            }
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            openFileDialog.Multiselect = false;
            DialogResult result = openFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                img.ImageLocation = openFileDialog.FileName;
            }
        }

        private void addNews()
        {
            News news = new News();
            news.Title = tbxTitle.Text.Trim();
            news.TitleJP = tbxTitleJP.Text.Trim();
            if (news.TitleJP.Equals("")) news.TitleJP = news.Title;
            news.AdminID = main.ad.ID;
            news.PostDate = DateTime.Today;
            string pathImg = Common.URL_SAVE_NEWS + Common.DEFAULT_NEWS_IMAGE_NAME;
            news.Image = pathImg;
            news.Content = tbxContent.Text.Trim();
            news.ContentJP = tbxContentJP.Text.Trim();
            if (news.ContentJP.Equals("")) news.ContentJP = news.Content;
            
            string message;
            BOLNews bolNews = new BOLNews();
            if (bolNews.addNews(news, out message) == true)
            {
                main.LoadTabPage_MarketNews();
                this.Close();
            }
            else
            {
                lblError.Text = message;
            }
        }

        private void editNews()
        {
            News news = new News();
            news.ID = newsID;
            news.Title = tbxTitle.Text.Trim();
            news.TitleJP = tbxTitleJP.Text.Trim();
            if (news.TitleJP.Equals("")) news.TitleJP = news.Title;
            news.AdminID = main.ad.ID;
            news.PostDate = DateTime.Today;
            string path = Common.URL_NEWS + Path.GetFileName(img.ImageLocation);
            string pathImg = Common.URL_SAVE_NEWS + Path.GetFileName(img.ImageLocation);
            if (openFileDialog.FileName.Equals("") == false)
            {
                pathImg = Common.URL_SAVE_NEWS + news.ID + Path.GetExtension(openFileDialog.FileName);
                path = Common.URL_NEWS + news.ID + Path.GetExtension(openFileDialog.FileName);
                img.Image.Save(path);
            }
            img.ImageLocation = path;
            news.Image = pathImg;
            news.Content = tbxContent.Text.Trim();
            news.ContentJP = tbxContentJP.Text.Trim();
            if (news.ContentJP.Equals("")) news.ContentJP = news.Content;
            news.ID = newsID;

            string message;
            BOLNews bolNews = new BOLNews();
            if (bolNews.editNews(news, out message) == true)
            {
                main.LoadTabPage_MarketNews();
                this.Close();
            }
            else
            {
                lblError.Text = message;
            }
        }

        private void validate()
        {
            flag = true;
            if ((tbxTitle.Text.Trim() == ""))
            {
                errorTitle.SetError(tbxTitle, Common.MESSAGE_ERROR_MUST_INPUT_DATA);
                flag = false;
            }
            else
            {
                errorTitle.Clear();
            }
            if (openFileDialog.FileName.Equals("") == false)
            {
                string ext = Path.GetExtension(openFileDialog.FileName);
                if (BOLCommon.checkFileImage(ext) == false)
                {
                    errorImage.SetError(img, Common.MESSAGE_ERROR_NOT_BE_IMAGE);
                    flag = false;
                }
                else
                {
                    errorImage.Clear();
                }
            }
            else
            {
                errorImage.Clear();
            }
            if ((tbxContent.Text.Trim() == ""))
            {
                errorContent.SetError(tbxContent, Common.MESSAGE_ERROR_MUST_INPUT_DATA);
                flag = false;
            }
            else
            {
                errorContent.Clear();
            }
        }
    }
}
